﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using ExamAnalysis.Model;
using System.Web;

namespace ExamAnalysis.Core
{
    public static class Menu
    {
        public enum Items
        {
            [Description("系统首页")]
            HomeMenu = 1,
            [Description("成绩查询")]
            ScoreMenu = 2,
            [Description("数据录入")]
            DataInputMenu = 3,
            [Description("数据维护")]
            DataMaintainMenu = 4,
            [Description("成绩分析")]
            ViewReportsMenu = 5,
            [Description("帐号管理")]
            AccountManageMenu = 6,
            [Description("系统管理")]
            RightsMaintainMenu = 7,
            [Description("云桌面")]
            DesktopMenu = 8,
            [Description("系统帮助")]
            ViewHelpMenu = 9,
            [Description("隐藏边栏")]
            HideMenu = 10
        };

        public enum Reports
        {
            [Description("超级管理员对比整体通用分析")]
            BasicCompareReport = 1,
            [Description("超级管理员对比学生通用分析")]
            BasicCompareStudentReport = 2,
            [Description("超级管理员总分整体通用分析报表")]
            BasicMacroReport = 3,
            [Description("超级管理员总分学生通用分析")]
            BasicMacroStudentReport = 4,
            [Description("超级管理员小分整体通用分析")]
            BasicMicroReport = 5,
            [Description("超级管理员小分学生通用分析")]
            BasicMicroStudentReport = 6,
            [Description("学生成绩查询")]
            MicroStudentReportForStudent = 7,
            [Description("班级多次考试成绩对比")]
            ACLCompareReport = 8,
            [Description("学生多次考试成绩对比")]
            ACLCompareStudentReport = 9,
            [Description("单科考试总体情况分析")]
            ACLPieReport = 10,
            [Description("卷面质量分析")]
            ACLMicroReport = 11,
            [Description("试题难度与区分度分析")]
            ACLMicroReportDifficulty = 12,
            [Description("认知目标分析/知识点分析")]
            ACLMicroReportKnowledge = 13,
            [Description("客观题作答情况分析")]
            ACLMicroReportChoose = 14,
            [Description("试题错因类别分析")]
            ACLMicroReportReason = 15,
            [Description("年级考试总体情况分析")]
            ACLMacroReportGroup = 16,
            [Description("成绩册")]
            ACLMacroStudentReport = 17,
            [Description("班级成绩对比")]
            ACLMacroReport = 18
        };

        public enum ReportType
        {
            [Description("整体分析报表")]
            MacroReports = 1,
            [Description("小分分析报表")]
            MicroReports = 2,
            [Description("对比分析报表")]
            CompareReports = 3,
        }

        public static string ResolveUrl(string relativeUrl)
        {
            if (relativeUrl == null) throw new ArgumentNullException("relativeUrl");

            if (relativeUrl.Length == 0 || relativeUrl[0] == '/' ||
                relativeUrl[0] == '\\') return relativeUrl;

            int idxOfScheme =
              relativeUrl.IndexOf(@"://", StringComparison.Ordinal);
            if (idxOfScheme != -1)
            {
                int idxOfQM = relativeUrl.IndexOf('?');
                if (idxOfQM == -1 || idxOfQM > idxOfScheme) return relativeUrl;
            }

            StringBuilder sbUrl = new StringBuilder();
            sbUrl.Append(HttpRuntime.AppDomainAppVirtualPath);
            if (sbUrl.Length == 0 || sbUrl[sbUrl.Length - 1] != '/') sbUrl.Append('/');

            // found question mark already? query string, do not touch!
            bool foundQM = false;
            bool foundSlash; // the latest char was a slash?
            if (relativeUrl.Length > 1
                && relativeUrl[0] == '~'
                && (relativeUrl[1] == '/' || relativeUrl[1] == '\\'))
            {
                relativeUrl = relativeUrl.Substring(2);
                foundSlash = true;
            }
            else foundSlash = false;
            foreach (char c in relativeUrl)
            {
                if (!foundQM)
                {
                    if (c == '?') foundQM = true;
                    else
                    {
                        if (c == '/' || c == '\\')
                        {
                            if (foundSlash) continue;
                            else
                            {
                                sbUrl.Append('/');
                                foundSlash = true;
                                continue;
                            }
                        }
                        else if (foundSlash) foundSlash = false;
                    }
                }
                sbUrl.Append(c);
            }

            return sbUrl.ToString();
        }

        public static List<ValueTextEntity> GetReportList(string roleName, ReportType reportType)
        {
            List<ValueTextEntity> results = new List<ValueTextEntity>();
            switch (reportType)
            {
                case ReportType.MicroReports:
                    if (Core.Rights.HasReportRightByRoles(roleName, (int)Core.Menu.Reports.BasicMicroReport))
                    {
                        results.Add(new ValueTextEntity("超级管理员小分整体通用分析", ResolveUrl("~/Reports/Basic/MicroReport.aspx?role=" + roleName)));
                    }
                    if (Core.Rights.HasReportRightByRoles(roleName, (int)Core.Menu.Reports.BasicMicroStudentReport))
                    {
                        results.Add(new ValueTextEntity("超级管理员小分学生通用分析", ResolveUrl("~/Reports/Basic/MicroStudentReport.aspx?role=" + roleName)));
                    }
                    if (Core.Rights.HasReportRightByRoles(roleName, (int)Core.Menu.Reports.ACLMicroReport))
                    {
                        results.Add(new ValueTextEntity("卷面质量分析", ResolveUrl("~/Reports/MicroReport/MicroReportbyACL.aspx?role=" + roleName)));
                    }
                    if (Core.Rights.HasReportRightByRoles(roleName, (int)Core.Menu.Reports.ACLMicroReportDifficulty))
                    {
                        results.Add(new ValueTextEntity("试题难度与区分度分析", ResolveUrl("~/Reports/MicroReport/MicroReportDifficultybyACL.aspx?role=" + roleName)));
                    }
                    if (Core.Rights.HasReportRightByRoles(roleName, (int)Core.Menu.Reports.ACLMicroReportKnowledge))
                    {
                        results.Add(new ValueTextEntity("知识点与认知目标分析", ResolveUrl("~/Reports/MicroReport/MicroReportKnowledgebyACL.aspx?role=" + roleName)));
                    }
                    if (Core.Rights.HasReportRightByRoles(roleName, (int)Core.Menu.Reports.ACLMicroReportChoose))
                    {
                        results.Add(new ValueTextEntity("客观题作答情况分析", ResolveUrl("~/Reports/MicroReport/MicroReportAnswerbyACL.aspx?role=" + roleName)));
                    }
                    if (Core.Rights.HasReportRightByRoles(roleName, (int)Core.Menu.Reports.ACLMicroReportReason))
                    {
                        results.Add(new ValueTextEntity("试题错因类别分析", ResolveUrl("~/Reports/MicroReport/MicroReportReasobyACL.aspx?role=" + roleName)));
                    }
                    break;
                case ReportType.MacroReports:
                    if (Core.Rights.HasReportRightByRoles(roleName, (int)Core.Menu.Reports.BasicMacroReport))
                    {
                        results.Add(new ValueTextEntity("超级管理员总分整体通用分析",  ResolveUrl("~/Reports/Basic/MacroReport.aspx?role=" + roleName)));
                    }
                    if (Core.Rights.HasReportRightByRoles(roleName, (int)Core.Menu.Reports.BasicMacroStudentReport))
                    {
                        results.Add(new ValueTextEntity("超级管理员总分学生通用分析", ResolveUrl("~/Reports/Basic/MacroStudentReport.aspx?role=" + roleName)));
                    }
                    if (Core.Rights.HasReportRightByRoles(roleName, (int)Core.Menu.Reports.ACLMacroReportGroup))
                    {
                        results.Add(new ValueTextEntity("年级考试总体情况分析", ResolveUrl("~/Reports/MacroReport/ClassMacroReport.aspx?role=" + roleName)));
                    }
                    if (Core.Rights.HasReportRightByRoles(roleName, (int)Core.Menu.Reports.ACLPieReport))
                    {
                        results.Add(new ValueTextEntity("单科考试总体情况分析", ResolveUrl("~/Reports/MacroReport/PicReport.aspx?role=" + roleName)));
                    }
                    if (Core.Rights.HasReportRightByRoles(roleName, (int)Core.Menu.Reports.ACLMacroStudentReport))
                    {
                        results.Add(new ValueTextEntity("成绩册", ResolveUrl("~/Reports/MacroReport/ScoreBook.aspx?role=" + roleName)));
                    }
                    if (Core.Rights.HasReportRightByRoles(roleName, (int)Core.Menu.Reports.ACLMacroReport))
                    {
                        results.Add(new ValueTextEntity("班级成绩对比", ResolveUrl("~/Reports/MacroReport/ClassMacroReport.aspx?role=" + roleName)));
                    }
                    break;
                case ReportType.CompareReports:
                    if (Core.Rights.HasReportRightByRoles(roleName, (int)Core.Menu.Reports.BasicCompareReport))
                    {
                        results.Add(new ValueTextEntity("超级管理员对比整体通用分析", ResolveUrl("~/Reports/Basic/CompareReport.aspx?role=" + roleName)));
                    }
                    if (Core.Rights.HasReportRightByRoles(roleName, (int)Core.Menu.Reports.BasicCompareStudentReport))
                    {
                        results.Add(new ValueTextEntity("超级管理员对比学生通用分析", ResolveUrl("~/Reports/Basic/CompareStudentReport.aspx?role=" + roleName)));
                    }
                    if (Core.Rights.HasReportRightByRoles(roleName, (int)Core.Menu.Reports.ACLCompareReport))
                    {
                        results.Add(new ValueTextEntity("班级多次考试成绩对比", ResolveUrl("~/Reports/CompareReport/CompareReportbyACL.aspx?role=" + roleName)));
                    }
                    if (Core.Rights.HasReportRightByRoles(roleName, (int)Core.Menu.Reports.ACLCompareStudentReport))
                    {
                        results.Add(new ValueTextEntity("学生多次考试成绩对比", ResolveUrl("~~/Reports/CompareReport/CompareStudentReportbyACL.aspx?role=" + roleName)));
                    }
                    break;
            }
            return results;
        }
    }
}
